package com.wwy.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 王伟羽
 * @date 2024/3/14 10:57
 */
@Component
public class RouterConsumer {

    @RabbitListener(queues = "router_queue_one")
    public void getTopicMessageOne(@Payload Message message, @Header(AmqpHeaders.CHANNEL) Channel channel) {
        try {
            // 获取消息内容
            String messageBody = new String(message.getBody());
            System.out.println("队列一收到消息："+messageBody);
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag, false);
            System.out.println("队列一收到ack");
        } catch (Exception e) {
            // 如果处理消息时出现异常，可以拒绝消息
            Long deliveryTag = (Long) message.getMessageProperties().getDeliveryTag();
            if (!channel.isOpen()) {
                // 如果channel已经关闭，则无法执行basicNack或basicReject
                return;
            }
            try {
                channel.basicNack(deliveryTag, false, true); // 拒绝消息并重新放回队列
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            // 或者可以选择 basicReject 如果不需要重新放回队列
            // channel.basicReject(deliveryTag, true); // 拒绝消息并丢弃
        }
    }

    @RabbitListener(queues = "router_queue_two")
    public void getTopicMessageTwo(@Payload Message message, @Header(AmqpHeaders.CHANNEL) Channel channel) {
        try {
            // 获取消息内容
            String messageBody = new String(message.getBody());
            System.out.println("队列二收到消息："+messageBody);
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag, false);
            System.out.println("队列二收到ack");
        } catch (Exception e) {
            // 如果处理消息时出现异常，可以拒绝消息
            Long deliveryTag = (Long) message.getMessageProperties().getDeliveryTag();
            if (!channel.isOpen()) {
                // 如果channel已经关闭，则无法执行basicNack或basicReject
                return;
            }
            try {
                channel.basicNack(deliveryTag, false, true); // 拒绝消息并重新放回队列
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            // 或者可以选择 basicReject 如果不需要重新放回队列
            // channel.basicReject(deliveryTag, true); // 拒绝消息并丢弃
        }
    }

    @RabbitListener(queues = "router_queue_three")
    public void getTopicMessageThree(@Payload Message message, @Header(AmqpHeaders.CHANNEL) Channel channel) {
        try {
            // 获取消息内容
            String messageBody = new String(message.getBody());
            System.out.println("队列三收到消息："+messageBody);
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag, false);
            System.out.println("队列三收到ack");
        } catch (Exception e) {
            // 如果处理消息时出现异常，可以拒绝消息
            Long deliveryTag = (Long) message.getMessageProperties().getDeliveryTag();
            if (!channel.isOpen()) {
                // 如果channel已经关闭，则无法执行basicNack或basicReject
                return;
            }
            try {
                channel.basicNack(deliveryTag, false, true); // 拒绝消息并重新放回队列
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            // 或者可以选择 basicReject 如果不需要重新放回队列
            // channel.basicReject(deliveryTag, true); // 拒绝消息并丢弃
        }
    }
}
